package com.ruoyi;

import com.tencent.SpeechClient;
import com.tencent.core.model.GlobalConfig;
import com.tencent.core.utils.ByteUtils;
import com.tencent.tts.model.SpeechSynthesisRequest;
import com.tencent.tts.model.SpeechSynthesisResponse;
import com.tencent.tts.service.SpeechSynthesisListener;
import com.tencent.tts.service.SpeechSynthesizer;
import com.tencent.tts.utils.LineSplitUtils;
import com.tencent.tts.utils.Ttsutils;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.UUID;

public class LongTextTtsExample {

    private static String codec = "pcm";
    // 智美	客服女声
    private static Integer type = 1003;
    private static int sampleRate = 16000;
    // 文件保存位置
    private static String save_path = "./audio/";

    public static void main(String[] args) throws IOException {
        GlobalConfig.ifLog = true;
        //从配置文件读取密钥
//        Properties props = new Properties();
//        props.load(new FileInputStream("../../config.properties"));
//        String appId = props.getProperty("appId");
//        String secretId = props.getProperty("secretId");
//        String secretKey = props.getProperty("secretKey");
        String appId = "1253542401";
        String secretId = "AKIDeBlFeuJ5cS8I8zQvz2wjruXkKLwkHDKI";
        String secretKey = "76vgL6n43dZJx6a2Mp7LTQAmiWbhet0x";


        //创建SpeechSynthesizerClient实例，目前是单例
        SpeechClient client = SpeechClient.newInstance(appId, secretId, secretKey);
        //初始化SpeechSynthesizerRequest，SpeechSynthesizerRequest包含请求参数
        SpeechSynthesisRequest request = SpeechSynthesisRequest.initialize();
        request.setCodec(codec);
        //request.setSampleRate(sampleRate);
        //request.setVolume(10);
        //request.setSpeed(2f);
        request.setVoiceType(type);
        //request.setPrimaryLanguage(2);
        //request.setSampleRate(null);
        //使用客户端client创建语音合成实例
        SpeechSynthesizer speechSynthesizer = client.newSpeechSynthesizer(request, new SpeechSynthesisListener(){

            @Override
            public void onComplete(SpeechSynthesisResponse response) {
//                Ttsutils.responsePcm2Wav(sampleRate, response.getAudio(), response.getSessionId());
                Ttsutils.responsePcm2Wav(sampleRate, response.getAudio(), save_path+ UUID.randomUUID().toString());
            }

            @Override
            public void onMessage(byte[] data) {
            }

            @Override
            public void onFail(SpeechSynthesisResponse exception) {

            }
        });
        //执行语音合成
        String ttsText = "制定待办清单。";
        speechSynthesizer.synthesisLongText(ttsText);
    }

}
